package com.fejal.dao;


import java.util.List;
import com.fejal.bean.StatusAluno;


public class StatusAlunoDAOHibernate implements StatusAlunoDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<StatusAluno> hibernateDAO = null;

	public StatusAlunoDAOHibernate() {
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getStatusDao();
	}

	@Override
	public boolean inserir(StatusAluno status) throws Exception {

		boolean ok = false;

		try {
			hibernateDAO.add(status);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@Override
	public boolean alterar(StatusAluno status) throws Exception {

		boolean ok = false;
	
		
		try {
			hibernateDAO.update(status);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}

	@Override
	public boolean excluir(StatusAluno status) throws Exception {

		boolean ok = false;

		try {
			hibernateDAO.remove(status);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StatusAluno> getSelecionar(String descricao) {
		String 	sql = "FROM StatusAluno WHERE codigoStatus <> 0 ";

		if (!(descricao.isEmpty())) {
			sql += "AND descricao like '" + descricao + "%' ";  
		}
		
		sql += "ORDER BY descricao";		
 
		List<StatusAluno> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@Override
	public StatusAluno getStatusAluno(int codigoStatus) {
		
		StatusAluno status = hibernateDAO.get(codigoStatus);
		return status;
	}

	@Override
	public List<StatusAluno> getListTodos() {
		
		List<StatusAluno> list = hibernateDAO.listAll();
		return list;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public boolean isExiste(StatusAluno status) {
		// SQL para verificar se a Status existe
		String 	sql = 	"FROM StatusAluno " +
				"WHERE codigoStatus = "+status.getCodigoStatus();

		List<StatusAluno> lista = hibernateDAO.getSession().createQuery(sql).list();

		if(lista.size()<1){
			return false;
		} else {
			return true;
		}
	}

}
